package com.example.bgmachine.bgmachine.waitviews;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.example.bgmachine.bgmachine.R;


public class ProducewaitCirclePgBar extends View {


    private Paint mBackPaint;
    private Paint mFrontPaint;
    private Paint mTextPaint;
    private Paint mCriclePaint;
    private float mStrokeWidth = 40;
    private float mHalfStrokeWidth = mStrokeWidth / 2;
    private float mRadius = 160;
    private RectF mRect;
    private int mProgress = 0;
    //目标值，想改多少就改多少
   // private int mTargetProgress = 90;
    private int mMax = 100;
    private int mWidth;
    private int mHeight;


    public ProducewaitCirclePgBar(Context context) {
        super(context);
        init();
    }

    public ProducewaitCirclePgBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ProducewaitCirclePgBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void  setMax(int maxs){
        mMax = maxs;
        invalidate();
    }

    public void  setCurrentProgress(int progress){
        mProgress = progress;
        invalidate();
    }
    public void addCurrentProgress(){
        mProgress++;
        invalidate();
    }
    public boolean isExitCurrentProgress(){
        if(mProgress >= mMax){
            return true;
        }
        return  false;
    }
    //完成相关参数初始化
    private void init() {
        mBackPaint = new Paint();
        mBackPaint.setColor(getResources().getColor(R.color.producewait_progress_circle_out_color_common));
        mBackPaint.setAntiAlias(true);
        mBackPaint.setStyle(Paint.Style.STROKE);
        mBackPaint.setStrokeWidth(mStrokeWidth);

        mFrontPaint = new Paint();
        mFrontPaint.setColor(getResources().getColor(R.color.producewait_progress_circle_in_color_common));
        mFrontPaint.setAntiAlias(true);
        mFrontPaint.setStyle(Paint.Style.STROKE);
        mFrontPaint.setStrokeWidth(mHalfStrokeWidth);


        mCriclePaint  = new Paint();
        mCriclePaint.setColor(Color.WHITE);
        mCriclePaint.setAntiAlias(true);
        mCriclePaint.setStrokeWidth(100);

        mTextPaint = new Paint();
        mTextPaint.setColor(getResources().getColor(R.color.producewait_progress_circle_in_color_common));
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(60);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
    }


    //重写测量大小的onMeasure方法和绘制View的核心方法onDraw()
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getRealSize(widthMeasureSpec);
        mHeight = getRealSize(heightMeasureSpec);
        setMeasuredDimension(mWidth, mHeight);

    }


    @Override
    protected void onDraw(Canvas canvas) {
        initRect();
        float angle = mProgress / (float) mMax * 360;
        canvas.drawCircle(mWidth / 2, mHeight / 2, mRadius, mBackPaint);
        canvas.drawArc(mRect, 90, angle, false, mFrontPaint);
        canvas.drawCircle(mWidth / 2, mHeight / 2, 100, mCriclePaint);
        canvas.drawText(mProgress * 100  /  mMax+ "%", mWidth / 2 + mHalfStrokeWidth, mHeight / 2 + mHalfStrokeWidth, mTextPaint);


    }

//    public void addTemp(){
//       // if (mProgress < mTargetProgress) {
//            mProgress += 1;
//            invalidate();
//       // }
//    }
    public int getRealSize(int measureSpec) {
        int result = 1;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);

        if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.UNSPECIFIED) {
            //自己计算
            result = (int) (mRadius * 2 + mStrokeWidth);
        } else {
            result = size;
        }

        return result;
    }

    private void initRect() {
        if (mRect == null) {
            mRect = new RectF();
            int viewSize = (int) (mRadius * 2);
            int left = (mWidth - viewSize) / 2;
            int top = (mHeight - viewSize) / 2;
            int right = left + viewSize;
            int bottom = top + viewSize;
            mRect.set(left, top, right, bottom);
        }
    }


}